/***************************************** 
 * Desc: 反向Mask
 * @Author: GSan
 * @Email: 2087653648@qq.com
 * CreateDate: 2020-07-24 11:09:24
*****************************************/

using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;

namespace Common.UGUI.Tool {

	public class UGUIReverseMask : Image {

        /// <summary>
        ///   <para>See IMaterialModifier.GetModifiedMaterial.</para>
        /// </summary>
        /// <param name="baseMaterial"></param>
        public override Material GetModifiedMaterial(Material baseMaterial) {
            Material baseMat = baseMaterial;
            if (this.m_ShouldRecalculateStencil) {
                Transform sortOverrideCanvas = MaskUtilities.FindRootSortOverrideCanvas(this.transform);
                this.m_StencilValue = !this.maskable ? 0 : MaskUtilities.GetStencilDepth(this.transform, sortOverrideCanvas);
                this.m_ShouldRecalculateStencil = false;
            }
            Mask component = this.GetComponent<Mask>();
            if (this.m_StencilValue > 0 && ((UnityEngine.Object)component == (UnityEngine.Object)null || !component.IsActive())) {
                Material material = StencilMaterial.Add(baseMat, (1 << this.m_StencilValue) - 1, StencilOp.Keep, CompareFunction.NotEqual, ColorWriteMask.All, (1 << this.m_StencilValue) - 1, 0);
                StencilMaterial.Remove(this.m_MaskMaterial);
                this.m_MaskMaterial = material;
                baseMat = this.m_MaskMaterial;
            }
            return baseMat;
        }

        public override bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) {

            if (!isActiveAndEnabled) {
                return true;
            }

            return !RectTransformUtility.RectangleContainsScreenPoint(rectTransform, sp, eventCamera);
        }
	}
}
